package team.jaman.autosell.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import team.jaman.autosell.dao.OrderdetailMapper;
import team.jaman.autosell.dao.OrdersMapper;
import team.jaman.autosell.dao.ProductMapper;
import team.jaman.autosell.entity.Orders;
import team.jaman.autosell.entity.Users;
import team.jaman.autosell.service.OrdersService;
import team.jaman.autosell.tools.OrdersCount;
import team.jaman.autosell.tools.PageData;
import team.jaman.autosell.tools.Param;
/**
 * 订单业务层
 * @author Administrator
 *
 */
@Service
public class OrdersServiceImpl implements OrdersService {

	@Autowired
	private OrdersMapper ordersMapper;
	
	@Autowired
	private OrderdetailMapper orderdetailMapper;
	
	@Autowired
	private ProductMapper productMapper;
	
	private static final Random random = new Random();
	private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
	
	@Override
	public PageData<Orders> getOrdersByLike(int page, int limit, String userkeyword) {
		// pageHelper分页
		Page<Orders> startPage = PageHelper.startPage(page, limit);
		// 根据用户关键字对订单进行模糊检索
		List<Orders> selectOrdersByUser = ordersMapper.selectOrdersByUser(userkeyword);
		// 总记录数
		int total = (int) startPage.getTotal();
		PageData<Orders> pageData = new PageData<>(selectOrdersByUser, total, limit, page);
		pageData.setCode(0);
		pageData.setMsg("");
		return pageData;
	}
	
	/**
	 * propagation = Propagation.REQUIRED 设置事务的传播性
	 * isolation 设置事物的隔离级别     Isolation.READ_COMMITTED 避免脏读
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
	public boolean insertOrders(Param param) {
		// 订单编号
		// 获取随机4位数
		int rannum = random.nextInt(8999) + 1000;		
		String orderId = simpleDateFormat.format(new Date()) + rannum;
		// 下单时间
		Date paytime = new Date();
		param.getOrders().setOrderId(orderId);
		param.getOrders().setPaytime(paytime);
		// 订单状态  1.下单成功
		param.getOrders().setOrderStatus(1);
		// 添加订单
		ordersMapper.insertSelective(param.getOrders());
		// 添加订单详情
		param.getOrderdetail().forEach(item->{
			item.setOrderId(orderId);
			orderdetailMapper.insertSelective(item);
		});
		// 修改商品库存
		param.getProduct().forEach(item->{
			productMapper.updateByPrimaryKeySelective(item);
		});
		return true;
	}

	@Override
	public List<OrdersCount> selectOrdersCountDay() {
		
		return ordersMapper.selectOrdersCountDay();
	}
	
	@Override
	public List<OrdersCount> selectOrdersCountMonth() {
		
		return ordersMapper.selectOrdersCountMonth();
	}

	@Override
	public List<Orders> getOrdersByUser(int userId) {
		
		return ordersMapper.selectByUserId(userId);
	}
	
}
